package com.itheima.ydd.controller;


import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService iSysMenuService;

    /**
     * 新增菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public ResultData addSysMenu(@RequestBody SysMenu sysMenu) {
        boolean save = iSysMenuService.save(sysMenu);
        return ResultData.ok(save ? "添加成功" : "添加失败", null);
    }

    /**
     * id删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delSysMenu(String id) {
        boolean b = iSysMenuService.deleteById(Integer.parseInt(id));
        if (b) {
            return ResultData.ok("删除成功", null);
        }
        return ResultData.error("有子菜单不能删除");
    }

    /**
     * 更新菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/update")
    public ResultData updateSysMenu(@RequestBody SysMenu sysMenu) {
        iSysMenuService.updateById(sysMenu);
        return ResultData.ok("更新成功", null);
    }

    /**
     * id查菜单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id) {
        SysMenu sysMenu = iSysMenuService.getById(Integer.parseInt(id));
        return ResultData.ok("", sysMenu);
    }

    /**
     * 所有菜单
     *
     * @return
     */
    @GetMapping("/allData")
    public ResultData findAll() {
        List<SysMenuDto> sysMenuDtoList = iSysMenuService.allData();
        return ResultData.ok("", sysMenuDtoList);
    }



}
